package com.example.demo;

import org.junit.jupiter.api.Test;

public class SplitTest {

    @Test
    public void splitBigPageIntoSmallPage() {
        int pageNo = 2;
        int exportPageSize = 2000;
        int total = 2511;
        int batchSize = 100;
        // 按分页参数pageNo、exportPageSize，拆分成小批次，计算出每次查询的起始位置和查询数量，考虑到最后一页不足batchSize的情况
        splitAndPrint(pageNo, exportPageSize, total, batchSize);
    }

    private void splitAndPrint(int pageNo, int exportPageSize, int total, int batchSize) {
        int offset = (pageNo - 1) * exportPageSize;
        int to = Math.min(offset + exportPageSize, total);
        for (; offset < to; offset+= batchSize) {
            int left = offset + batchSize - to;
            int limit = left < 0 ? batchSize : Math.abs(left - batchSize);
            System.out.printf("start: %d, limit: %d%n", offset, limit);
        }
    }

}
